#ifndef videoplay_CPP
#define videoplay_CPP

#include "video_play.h"
#include <chrono>
#include <thread>

videoplay::videoplay(QObject *parent) :
    QObject(parent), stopped(false), running(false), stopped2(false), running2(false), videoDelay(0)
{}

void videoplay::play_video()
{
    if (!running || stopped) return;

    emit showVideo();

    /* Limit play_video loop speed */
    if (videoDelay != 0)
        std::this_thread::sleep_for(std::chrono::milliseconds(videoDelay));

    /* Use QueuedConnection to give other threads a chance to be processed before next play_videoQt::QueuedConnection */
    QMetaObject::invokeMethod(this, "play_video", Qt::QueuedConnection);
}

void videoplay::StopVideo()
{
    stopped = true;
    running = false;
}

void videoplay::StartVideo()
{
    stopped = false;
    running = true;

    play_video();
}

void videoplay::play_video_2()
{
    if (!running2 || stopped2) return;

    emit showVideo2();

    /* Limit play_video loop speed */
    if (videoDelay != 0)
        std::this_thread::sleep_for(std::chrono::milliseconds(videoDelay));

    /* Use QueuedConnection to give other threads a chance to be processed before next play_video */
    QMetaObject::invokeMethod(this, "play_video_2", Qt::QueuedConnection);
    std::this_thread::sleep_for(std::chrono::milliseconds(50));
}

void videoplay::StopVideo_2()
{
    stopped2 = true;
    running2 = false;
}

void videoplay::StartVideo_2()
{
    stopped2 = false;
    running2 = true;

    play_video_2();
}

void videoplay::setDelayMS(unsigned int delay)
{
    videoDelay = delay;
}

#endif // videoplay_CPP
